package com.demo.controller;

import com.demo.common.Result;
import com.demo.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 成绩管理控制器
 */
@RestController
@RequestMapping("/grade")
@CrossOrigin
public class GradeController {
    
    @Autowired
    private GradeService gradeService;
    
    /**
     * 教师给学生评分
     * @param studentCourseId 学生选课ID
     * @param usualGrade 平时成绩
     * @param examGrade 考试成绩
     * @return 评分结果
     */
    @PostMapping("/submit")
    public Result<String> submitGrade(@RequestParam Long studentCourseId,
                                      @RequestParam(required = false) BigDecimal usualGrade,
                                      @RequestParam(required = false) BigDecimal examGrade,
                                      @RequestParam(required = false) String remark) {
        try {
            boolean success = gradeService.submitGrade(studentCourseId, usualGrade, examGrade, remark);
            if (success) {
                return Result.success("评分成功");
            } else {
                return Result.error("评分失败");
            }
        } catch (Exception e) {
            return Result.error("评分失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量评分
     * @param grades 成绩列表
     * @return 评分结果
     */
    @PostMapping("/batch-submit")
    public Result<String> batchSubmitGrades(@RequestBody List<Map<String, Object>> grades) {
        try {
            int successCount = gradeService.batchSubmitGrades(grades);
            return Result.success("成功评分 " + successCount + " 条记录");
        } catch (Exception e) {
            return Result.error("批量评分失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取课程的学生成绩列表（教师查看）
     * @param courseId 课程ID
     * @param semester 学期
     * @return 学生成绩列表
     */
    @GetMapping("/course/{courseId}")
    public Result<List<Map<String, Object>>> getCourseGrades(@PathVariable Long courseId,
                                                            @RequestParam(required = false) String semester) {
        try {
            List<Map<String, Object>> grades = gradeService.getCourseGrades(courseId, semester);
            return Result.success(grades);
        } catch (Exception e) {
            return Result.error("获取成绩列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取教师负责课程的学生成绩列表
     * @param teacherId 教师ID
     * @param semester 学期
     * @return 学生成绩列表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<List<Map<String, Object>>> getTeacherCourseGrades(@PathVariable Long teacherId,
                                                                   @RequestParam(required = false) String semester) {
        try {
            List<Map<String, Object>> grades = gradeService.getTeacherCourseGrades(teacherId, semester);
            return Result.success(grades);
        } catch (Exception e) {
            return Result.error("获取成绩列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新成绩
     * @param studentCourseId 学生选课ID
     * @param usualGrade 平时成绩
     * @param examGrade 考试成绩
     * @param remark 备注
     * @return 更新结果
     */
    @PutMapping("/update")
    public Result<String> updateGrade(@RequestParam Long studentCourseId,
                                      @RequestParam(required = false) BigDecimal usualGrade,
                                      @RequestParam(required = false) BigDecimal examGrade,
                                      @RequestParam(required = false) String remark) {
        try {
            boolean success = gradeService.updateGrade(studentCourseId, usualGrade, examGrade, remark);
            if (success) {
                return Result.success("更新成绩成功");
            } else {
                return Result.error("更新成绩失败");
            }
        } catch (Exception e) {
            return Result.error("更新成绩失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除成绩
     * @param studentCourseId 学生选课ID
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public Result<String> deleteGrade(@RequestParam Long studentCourseId) {
        try {
            boolean success = gradeService.deleteGrade(studentCourseId);
            if (success) {
                return Result.success("删除成绩成功");
            } else {
                return Result.error("删除成绩失败");
            }
        } catch (Exception e) {
            return Result.error("删除成绩失败：" + e.getMessage());
        }
    }
}

